package cn.edu.ujn.booksshop.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.User;
import cn.edu.ujn.booksshop.service.IUserService;
import cn.edu.ujn.util.Page;

@Controller
public class UserController {

	@Autowired
	private IUserService userService;

	@Autowired
	private User userDao;

	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(User user, Model model, HttpSession session) {
		User user1 = userService.findUser(user);
		if (user1 != null && !user1.getIsdeleted()) {
			session.setAttribute("USER_SESSION", user1);
			return "sumup";
		} else if (user1 != null) {
			model.addAttribute("msg", "账号已经注销");
		} else {
			model.addAttribute("msg", "账号或密码错误，请重新输入！");
		}
		return "login";
	}

	@RequestMapping(value = "/userManagement.action", method = RequestMethod.POST)
	public String userManagement() {
		return "userManagement";
	}

	@RequestMapping(value = "/bookManagement.action", method = RequestMethod.POST)
	public String bookManagement() {
		return "bookManagement";
	}
	//凯
	@RequestMapping(value = "/review.action", method = RequestMethod.POST)
	public String reviewManagement() {
		return "review";
	}
	@RequestMapping(value = "/payment.action", method = RequestMethod.POST)
	public String paymentManagement() {
		return "payment";
	}
	//huang
	@RequestMapping(value = "/authors.action", method = RequestMethod.POST)
	public String authorsManagement() {
		return "authors";
	}
	@RequestMapping(value = "/stocks.action", method = RequestMethod.POST)
	public String stocksManagement() {
		return "stocks";
	}
	//chichi
	@RequestMapping(value = "/order.action", method = RequestMethod.POST)
	public String orderManagement() {
		return "order";
	}
	//jia
	@RequestMapping(value = "/cartDetail.action", method = RequestMethod.POST)
	public String cartDetailManagement() {
		return "cartDetail";
	}
	@RequestMapping(value = "/shoppingCart.action", method = RequestMethod.POST)
	public String shoppingCartManagement() {
		return "shoppingCart";
	}
	@RequestMapping(value = "/user/list.action")
	public String list(Model model, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "20") Integer rows) {
		Page<User> pages = this.userService.selectAll(page, rows);
		model.addAttribute("page", pages);
		return "userManagement";
	}

	@RequestMapping(value = "user/create.action")
	@ResponseBody
	public String add(User user) {
		user.setIsdeleted(false);
		int insert = this.userService.insertSelective(user);
		if(insert>0) {
			return "OK";
		}else {
			return "FAIL";
		}
	}

	@RequestMapping(value = "user/delete.action")
	@ResponseBody
	public String deleteByPrimaryKey(Integer id) {
		int deleteByPrimaryKey = this.userService.deleteByPrimaryKey(id);
		if(deleteByPrimaryKey>0)
		{
			return "OK";
		}else {
			return "FAIL";
		}

	}

	@RequestMapping(value = "/user/update.action")
	@ResponseBody
	public String update(User user) {
		int selective = this.userService.updateByPrimaryKeySelective(user);
		if (selective > 0) {
			return "OK";
		} else {
			return "Fail";
		}
	}

	@RequestMapping("/user/getUserById.action")
	@ResponseBody
	public User getUserById(Integer id) {
		User user = userService.selectByPrimaryKey(id);
		return user;
	}

}
